<?php
namespace Poster\Controller;
use Admin\Controller\InitController;
if (!defined('MODULE_CACHE')) define('MODULE_CACHE', DATA_PATH.'caches_poster/');
class PosterSpaceController extends InitController{
	public function _initialize(){
		parent::_initialize();
		$this->siteid = I('siteid','1','intval');
		$this->db = D('PosterSpace');
		$this->module_db = D('Module');
	}
	public function init(){
		$pagecurr = max(1,I('page',0,'intval'));
		$pagesize = 10;
		$sqlMap = array();
		$count = $this->db->where($sqlMap)->count();
		$lists = $this->db->where($sqlMap)->page($pagecurr,$pagesize)->select();
		$pages = page($count,$pagesize);
		$types = get_types();
		$big_menu = array('javascript:window.top.art.dialog({id:\'add\',iframe:\''.U('add').'\', title:\''.L('add_space').'\', width:\'540\', height:\'320\', lock:true}, function(){var d = window.top.art.dialog({id:\'add\'}).data.iframe;var form = d.document.getElementById(\'dosubmit\');form.click();return false;}, function(){window.top.art.dialog({id:\'add\'}).close()});void(0);', L('add_space'));
		include $this->admin_tpl('poster_space_list');
	}
	/**
	 * 添加版位
	 */
	public function add(){
		if(submitcheck('dosubmit')){
			$space = (array) I('space');
			$setting =(array) I('setting');
			$space['setting'] = array2string($setting);
			$space['siteid'] = $this->siteid;
			$result = $this->db->update($space);
			if($result){
				if ($space['type']=='code') {//代码调用
					$path = '<?php echo show_ad('.$space['siteid'].','. $result.');?>';
				} else {
					$path = 'poster_js/'.$result.'.js';
				}
				$sqlMap = array();
				$sqlMap['path'] = $path;
				$rs = $this->db->where(array('spaceid'=>$result,'siteid'=>$this->siteid))->save($sqlMap);
				if(!$rs){
					$this->error('添加失败');
				}
				$this->success('添加成功');
			}
		}else{
			$types = get_types();
			$poster_template = getcache('poster_template_'.$this->siteid, 'commons');
			$show_header = $show_validator = true;
			$form = new \Common\Library\form();
			include $this->admin_tpl('poster_space_add');
		}
	}
	/**
	 * 修改广告版位
	 */
	public function edit(){
		$spaceid = (int) I('spaceid');
		if($spaceid < 0){$this->error('参数错误');}
		if(submitcheck('dosubmit')){
			$space = (array) I('space');
			$setting = (array) I('setting');
			$space['spaceid'] = $spaceid;
			$space['setting'] = array2string($setting);
			if ($space['type']=='code') {
				$space['path'] = '<?php echo show_ad('.$this->siteid.','.$spaceid.');?>';
			} else {
				$space['path'] = 'poster_js/'.$spaceid.'.js';
			} 
			if (isset($_POST['old_type']) && $_POST['old_type']!=$space['type']) {
				D('Poster')->where(array('spaceid'=>$spaceid))->delete();
				$space['items'] = 0;
			}
			$result = $this->db->update($space);
			if(!$result){
				$this->error('修改失败');
			}
			$this->success('修改成功');
		}else{
			$info = $this->db->where(array('spaceid'=>$spaceid))->find();
			$setting = string2array($info['setting']);
			$poster_template = getcache('poster_template_'.$this->siteid, 'commons');
			$show_header = $show_validator = true;
			$types = get_types();
			$form = new \Common\Library\form();
			include $this->admin_tpl('poster_space_edit');
		}
	}
	/**
	 * 删除广告位
	 */
	public function delete($spaceid = array()){
		$spaceid = (array) $spaceid;
		if(empty($spaceid)){
			$this->error('参数错误');
		}
		if(!empty($spaceid)){
			foreach($spaceid as $id){
				$id = (int) $id;
				$this->db->where(array('spaceid'=>$id))->delete();
			}
			$this->success('操作成功');
		}else{
			$this->error('操作失败');
		}
	}
	/**
	 * 广告模板配置
	 */
	public function poster_template(){
		$tpl_root = MODULE_PATH ;
		$templatedir = MODULE_PATH.'Fields/';
		$poster_template = getcache('poster_template_'.$this->siteid, 'commons');
		$templates = glob($templatedir.'*.html');
		if (is_array($templates) && !empty($templates)) {
			foreach ($templates as $k => $tem) {
				$templates[$k] = basename($tem, ".html");
			}
		}
		$big_menu = array('javascript:window.top.art.dialog({id:\'add\',iframe:\''.U('add').'\', title:\''.L('add_space').'\', width:\'540\', height:\'320\', lock:true}, function(){var d = window.top.art.dialog({id:\'add\'}).data.iframe;var form = d.document.getElementById(\'dosubmit\');form.click();return false;}, function(){window.top.art.dialog({id:\'add\'}).close()});void(0);', L('add_space'));
		include $this->admin_tpl('poster_template');
	}
	/**
	 * 删除模板
	 */
	public function public_tempate_del(){
		$id = I('id');
		if(!isset($id)){$this->error('参数错误');}
		$poster_template = getcache('poster_template_'.$this->siteid, 'commons');
		if ($poster_template[$id]) {
			unset($poster_template[$id]);
		}
		setcache('poster_template_'.$this->siteid, $poster_template, 'commons');
		$this->success('操作成功');
	}
	/**
	 * 配置模板
	 */
	public function public_template_setting() {
		$siteid = $this->siteid;
		$poster_template = getcache('poster_template_'.$siteid, 'commons');
		if (submitcheck('dosubmit')) {
			if (is_array($_POST['info']['type']) && !empty($_POST['info']['type'])) {
				$type2name = array('images'=>L('photo'), 'flash'=>L('flash'), 'text'=>L('title'));
				$type = array();
				foreach ($_POST['info']['type'] as $t) {
					if (in_array($t, array('images', 'flash', 'text'))) {
						$type[$t] = $type2name[$t];
					} else {
						continue;
					}
				}
			}
			unset($_POST['info']['type']);
			$_POST['info']['type'] = $type;
			$poster_template[$_POST['template']] = $_POST['info'];
			//代码广告模式
			$code = array('code' => array ( 'name' => '代码广告','type' => array ( 'text' => '代码',),'num' => 1, 'iscore' => 1, 'option' => 0,),);
			$poster_template = array_merge($poster_template,$code);
			setcache('poster_template_'.$siteid, $poster_template, 'commons');
			$this->success(L('setting_success'));
		} else {
			if (!isset($_GET['template'])) {
				$this->error(L('illegal_parameters'));
			} else {
				$template = $_GET['template'];
			}
			if ($poster_template[$template]) {
				$info = $poster_template[$template];
				if (is_array($info['type']) && !empty($info['type'])) {
					$type = array();
					$type = array_keys($info['type']);
					unset($info['type']);
					$info['type'] = $type;
				}
			}
			include $this->admin_tpl('template_setting');
		}
	}
	
	/**
	 * 判断是否广告版位存在
	 */
	public function public_check_space(){
		$spaceid = (int) I('spaceid');
		$rs = $this->db->where(array('spaceid'=>$spaceid))->count();
		if($rs){
			echo "0";
		}else{
			echo "1";
		}
	}
	/**
	 * 模板配置
	 */
	public function setting(){
		if(submitcheck('dosubmit')){
			$sqlMap = array();
			$setting = (array) I('setting');
			//设置缓存
			$poster = serialize($setting);
			setcache('poster', $poster, 'commons');
			$sqlMap['setting'] = $poster;
			$result  = $this->module_db->where(array('module'=>'Poster'))->save($sqlMap);
			if(!$result){
				$this->error('配置失败');
			}
			$this->success('配置成功');
		}else{
			$setting = getcache('poster', 'commons');
			$setting = unserialize($setting);
			extract($setting);
			include $this->admin_tpl('setting');
		}
	}
	/**
	 * 广告版位预览
	 */
	public function public_preview(){
			if (is_numeric($_GET['spaceid'])) {
				$spaceid = intval($_GET['spaceid']);
				$r = $this->db->where(array('spaceid'=>$spaceid, 'siteid'=>$this->siteid))->find();
				$scheme = $_SERVER['SERVER_PORT'] == '443' ? 'https://' : 'http://';
				$rs = D('Poster')->where(array('spaceid'=>$r['spaceid'], 'siteid'=>$this->siteid))->field('setting')->find();
				$types = get_types();
				if ($r['type']=='code') {
					if ($rs['setting']) {
						$d = string2array($rs['setting']);
						$data = $d['code'];
					}
				} else {
					$path = JS_PATH.$r['path'];
				}
				include $this->admin_tpl('space_preview');
			}
		}
		/*
		 * 调用代码
		 */
		public function public_call(){
			$sid = (int) I('sid');
			if (!$sid) {$this->error('参数错误');};
			$r = $this->db->where(array('spaceid'=>$sid, 'siteid'=>$this->siteid))->find();
			extract($r);
			include $this->admin_tpl('space_call');
		}
		/**
		 * 重新生成js
		 */
		public function creat_js(){
			$page = max(1,I('page',0,'intval'));
			if($page == 1){
				$result = $this->db->where(array('disabled'=>0,'siteid'=>$this->siteid))->field('count(*) as num')->find();
				if($result['num']){
					$total = $result['num'];
					$pages = ceil($total / 20);
				}else{
					$pages = $page ? intval($page) : 0;
				}
				$offset = ($page - 1) * 20;
				$data = $this->db->where(array('disabled'=>0,'siteid'=>$this->siteid))->page($page,$offset)->order('spaceid ASC')->select();
				//调用Poster 里面的creat_js方法
				$poster = A(Poster/Poster);
				foreach ($data as $d){
					if($d['type'] != 'code'){
						$poster->creat_js($d['spaceid']);
					}else{
						continue;
					}
				} 
				$page++;
				if($page > $pages){
					$this->success(L('update_js_success'),U('init',array('menuid'=>MENUID)));
				}else{
					$this->error(L('update_js').'<font style="color:red">'.($page-1).'/'.$pages.'</font>',U('init',array('menuid'=>MENUID)));
				}
			}
		}
}